/**
 * Created by apple on 2017/12/28.
 */
var express = require('express');
var router = express.Router();

var FreezeMoneyRouter = module.exports;
var UserToken = require('../db/user_tokens.js');
var FreezeMoneyDao = require('../dao/freeze_money');
var UserFreezeMoneyDao = require('../dao/user_freeze_money');



FreezeMoneyRouter.getFreezeList = function (req,res) {
    var par = paramAll(req);
    UserToken.checkLogin(par.token,function (err,userInfo) {
        if(err) return res.json(new ERR(err, 400));
        if(!userInfo) return res.json(new ERR('请先登录', 403));
        console.log(par.token);
        par.uid = userInfo.uid;
        FreezeMoneyDao.getFreezeList(par,function (err,result) {
            if(err) return res.json(new ERR(err, 400));
            res.json(new PKG(result));
        });
    });
}
router.post('/getFreezeList', FreezeMoneyRouter.getFreezeList);



FreezeMoneyRouter.unfreezeMoney = function (req,res) {
    var par = paramAll(req);
    if (!par.app_sid) return res.json(new ERR('产品标识不为空', 400));
    UserToken.checkAdminLogin(par.token,par.app_sid,function (err,errCode,userInfo) {
        if(err) return res.json(new ERR(err, errCode));
        if (!par.uid) return res.json(new ERR('用户标识不为空', 400));
        if (par.vip_sid == 'jihuo') par.unfreeze_sid = 'wtk';
        if (par.vip_sid == 'firstVip') par.unfreeze_sid = 'jwtk';
        UserFreezeMoneyDao.unfreezeMoney(par,function (err,result) {
            if(err) return res.json(new ERR(err, 400));
            res.json(new PKG(result));
        });
    });
}
router.post('/unfreezeMoney', FreezeMoneyRouter.unfreezeMoney);

FreezeMoneyRouter.unfreezeMoneyBySrv = function (req,res) {
    var par = paramAll(req);
    if(!checkSrvConnection(par)) return res.json(new ERR('访问受限',400));
    if (!par.app_sid) return res.json(new ERR('产品标识不为空', 400));
    if (!par.uid) return res.json(new ERR('用户标识不为空', 400));
    if (par.vip_sid == 'jihuo') par.unfreeze_sid = 'wtk';
    if (par.vip_sid == 'firstVip') par.unfreeze_sid = 'jwtk';
    UserFreezeMoneyDao.unfreezeMoney(par,function (err,result) {
        if(err) return res.json(new ERR(err, 400));
        res.json(new PKG(result));
    });
}
router.post('/unfreezeMoneyBySrv', FreezeMoneyRouter.unfreezeMoneyBySrv);


FreezeMoneyRouter.unfreezeMoneyByAdm = function (req,res) {
    var par = paramAll(req);
    if (par.admSrv != 'srv_hello') return res.json(new ERR('访问受限', 400));
    if (!par.app_sid) return res.json(new ERR('产品标识不为空', 400));
    if (!par.uid) return res.json(new ERR('用户标识不为空', 400));
    if (par.vip_sid == 'jihuo') par.unfreeze_sid = 'wtk';
    if (par.vip_sid == 'firstVip') par.unfreeze_sid = 'jwtk';
    UserFreezeMoneyDao.unfreezeMoney(par,function (err,result) {
        if(err) return res.json(new ERR(err, 400));
        res.json(new PKG(result));
    });
}
router.post('/unfreezeMoneyByAdm', FreezeMoneyRouter.unfreezeMoneyByAdm);


FreezeMoneyRouter.start = function (req,res) {
    var par = paramAll(req);
    if (par.key != 'hello_word') return res.json(new ERR('ee', 400));
    FreezeMoneyDao.start(par,function (err,result) {
        if(err) return res.json(new ERR(err, 400));
        res.json(new PKG(result));
    });
}
router.post('/start', FreezeMoneyRouter.start);














module.exports = router;